package com.brsya.movie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.brsya.base.base.BaseLayoutFragment;
import com.brsya.base.base.Contents;
import com.brsya.base.util.UiUtil;
import com.brsya.movie.R;
import com.brsya.movie.activity.SearchActivity;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/6  16:55
 * Description:
 * 发现Fragment
 */
public class FindFragment extends BaseLayoutFragment {
    public static final int SELECT_LIST = 0;
    public static final int SELECT_RANKING = 1;
    public static final int SELECT_CATEGORY = 2;

    private LinearLayout llTitleBackBox;
    private int initSelectIndex;
    private LinearLayout llFindPage;
    private SlidingTabLayout tlMovieTitle;
    private ViewPager vpFindBox;


    // 标题
    private final String[] tabs = {"片单", "排行榜", "分类"};
    /**
     * Fragment列表
     */
    private List<Fragment> tabFragmentList;
    private String movieType;
    private String movieSort;
    private boolean isBack;

    private View.OnClickListener onBackClickListener;
    private View vFindSearchBox;

    public static FindFragment newInstance() {
        return newInstance(SELECT_LIST, null, null, false);
    }

    public static FindFragment newInstance(int selectIndex, String movieType, String movieSort, boolean isBack) {

        Bundle args = new Bundle();
        args.putInt("selectIndex", selectIndex);
        args.putBoolean("isBack", isBack);
        if (movieType != null) {
            args.putString(Contents.INTENT_MOVIE_TYPE, movieType);
        }
        if (movieSort != null) {
            args.putString(Contents.INTENT_MOVIE_SORT, movieSort);
        }

        FindFragment fragment = new FindFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setArguments(@Nullable Bundle args) {
        super.setArguments(args);
        if (args != null) {
            isBack = args.getBoolean("isBack", false);
            initSelectIndex = args.getInt("selectIndex", 0);
            if (initSelectIndex >= tabs.length) {
                initSelectIndex = tabs.length;
            }
            if (args.getString(Contents.INTENT_MOVIE_TYPE) != null) {
                movieType = args.getString(Contents.INTENT_MOVIE_TYPE);
            }
            if (args.getString(Contents.INTENT_MOVIE_SORT) != null) {
                movieSort = args.getString(Contents.INTENT_MOVIE_SORT);
            }
        }
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_find);

        llTitleBackBox = findViewById(R.id.ll_title_back_box);
        llFindPage = findViewById(R.id.ll_find_page);
        tlMovieTitle = findViewById(R.id.tl_movie_title);
        vpFindBox = findViewById(R.id.vp_find_box);
        vFindSearchBox = findViewById(R.id.v_find_search_box);
    }

    @Override
    protected void initData() {
        UiUtil.setStatusPadding(getContext(), llFindPage);
        tabFragmentList = new ArrayList<>();
        tabFragmentList.add(InventoryFragment.newInstance(-1));
        tabFragmentList.add(RankingFragment.newInstance(0));
        tabFragmentList.add(CategoryFragment.newInstance(movieType, movieSort));

        if (isBack) {
            llTitleBackBox.setVisibility(View.VISIBLE);
            llTitleBackBox.setOnClickListener(onBackClickListener);
        }


        vpFindBox.setOffscreenPageLimit(3);
        vpFindBox.setAdapter(new FragmentPagerAdapter(getChildFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return tabFragmentList.get(position);
            }

            @Override
            public int getCount() {
                return tabs.length;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabs[position];
            }
        });

        tlMovieTitle.setViewPager(vpFindBox);

        vpFindBox.setCurrentItem(initSelectIndex);


        vFindSearchBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
    }


    /**
     * 设置点击返回键回调
     *
     * @param clickListener 点击回调
     */
    public void setOnBackListener(View.OnClickListener clickListener) {
        this.onBackClickListener = clickListener;
    }
}
